package cn.gcyl.module.biz.controller.admin.marketinformation;

import cn.gcyl.framework.apilog.core.annotation.ApiAccessLog;
import cn.gcyl.framework.common.pojo.CommonResult;
import cn.gcyl.framework.common.pojo.PageParam;
import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.framework.excel.core.util.ExcelUtils;
import cn.gcyl.module.biz.controller.admin.marketinformation.vo.*;
import cn.gcyl.module.biz.dal.dataobject.marketinformation.MarketInformationDO;
import cn.gcyl.module.biz.service.marketinformation.MarketInformationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.gcyl.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gcyl.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 市场资讯")
@RestController
@RequestMapping("/biz/market-information")
@Validated
public class MarketInformationController {

    @Resource
    private MarketInformationService marketInformationService;

    @PostMapping("/create")
    @Operation(summary = "创建市场资讯")
//    @PreAuthorize("@ss.hasPermission('biz:market-information:create')")
    public CommonResult<Integer> createMarketInformation(@Valid @RequestBody MarketInformationSaveReqVO createReqVO) {
        return success(marketInformationService.createMarketInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新市场资讯")
//    @PreAuthorize("@ss.hasPermission('biz:market-information:update')")
    public CommonResult<Boolean> updateMarketInformation(@Valid @RequestBody MarketInformationSaveReqVO updateReqVO) {
        marketInformationService.updateMarketInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除市场资讯")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('biz:market-information:delete')")
    public CommonResult<Boolean> deleteMarketInformation(@RequestParam("id") Integer id) {
        marketInformationService.deleteMarketInformation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除市场资讯")
//                @PreAuthorize("@ss.hasPermission('biz:market-information:delete')")
    public CommonResult<Boolean> deleteMarketInformationList(@RequestParam("ids") List<Integer> ids) {
        marketInformationService.deleteMarketInformationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得市场资讯")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('biz:market-information:query')")
    public CommonResult<MarketInformationRespVO> getMarketInformation(@RequestParam("id") Integer id) {
        MarketInformationDO marketInformation = marketInformationService.getMarketInformation(id);
        return success(BeanUtils.toBean(marketInformation, MarketInformationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得市场资讯分页")
//    @PreAuthorize("@ss.hasPermission('biz:market-information:query')")
    public CommonResult<PageResult<MarketInformationRespVO>> getMarketInformationPage(@Valid MarketInformationPageReqVO pageReqVO) {
        PageResult<MarketInformationDO> pageResult = marketInformationService.getMarketInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MarketInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出市场资讯 Excel")
//    @PreAuthorize("@ss.hasPermission('biz:market-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMarketInformationExcel(@Valid MarketInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MarketInformationDO> list = marketInformationService.getMarketInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "市场资讯.xls", "数据", MarketInformationRespVO.class,
                        BeanUtils.toBean(list, MarketInformationRespVO.class));
    }

}